package com.hmyd.hmyd_android.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class LPWebView extends WebView {
	public OnScrollChangeListener listener;

	public LPWebView(Context context) {
		super(context);
	}
	
	public LPWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public LPWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}


	@SuppressLint("NewApi")
	@Override
	public void onScreenStateChanged(int screenState) {
		super.onScreenStateChanged(screenState);
	}
	
	// 执行滑动监听
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		
		float contentHeight = getContentHeight() * getScale();	// webView高度
		float currentHeight = getHeight() + getScrollY();		// 当前webView高度
		
		if (Math.abs(contentHeight - currentHeight) < 1) {		// 已经到底
			listener.onPageEnd(l, t, oldl, oldt);
		} else if (getScrollY() == 0) {							// 已经到顶
			listener.onPageTop(l, t, oldl, oldt);
		} else {												// 滑动
			listener.onScrollChanged(l, t, oldl, oldt);
		}
		
	}

	// 设置滑动监听
	public void setOnScrollChangedListener(OnScrollChangeListener listener){
		this.listener = listener;
	}
	
	// 滑动监听接口
	public interface OnScrollChangeListener{
		public void onPageTop(int l, int t, int oldl, int oldt);
		public void onPageEnd(int l, int t, int oldl, int oldt);
		public void onScrollChanged(int l, int t, int oldl, int oldt);
	}
}
